home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / pxbud.zip / PXBUD.CPP < prev    next >
C/C++ Source or Header  |  1991-05-17  |  5KB  |  143 lines

  1. /*┌───────────────────────────────────────────────────────────────────────┐
  2.   │                                                                       │
  3.   │   Module:  PXBUD.CPP                                                  │
  4.   │   Author:  Rick Kligman                                               │
  5.   │   Purpose: To illustrate the syntax of PX_Buddy++                     │
  6.   │                                                                       │
  7.   │   Last Modified: 04-26-91 11:21am                                     │
  8.   └───────────────────────────────────────────────────────────────────────┘ */
  9.  
  10. #include <stdio.h>
  11. #include <iostream.h>
  12. #include <conio.h>
  13. #include "cust.h"
  14.  
  15. void  exit_program(void);
  16.  
  17.  
  18. Customer      cust;     // This will instatiate the Customer class and make
  19.                         // references to cust global
  20.  
  21. int   err;              // I like to use err to check for error codes
  22.  
  23. void main()
  24. {
  25.   int   i;
  26.   char  enterid [15];
  27.  
  28.   err = PXInit();
  29.   if ( err )
  30.       pdxerr(err, "Initializing Engine", TRUE);
  31.  
  32.   err = cust.open();    // open the customer table
  33.   if ( err )
  34.       pdxerr(err, "cust.open", TRUE);
  35.  
  36.   for (i = 0; i < 10; i++) {
  37.       cust.read_rec();      // this will read record and ALL fields
  38.  
  39.       // Next I will show you how to reference fields in the record
  40.       // buffer.
  41.  
  42.       cout << cust.custid << " " << cust.phone << " " << cust.date << "\n";
  43.  
  44.       // I will now move to the next record. The return value however
  45.       // from this function will only return an error when a REAL error
  46.       // occurs. By this I mean, PXRecNext() returns an error code for
  47.       // End/Beginning of table. I do not consider this an error and
  48.       // will return a 0 in this case. To find out whether the End/Beginning
  49.       // of table was reached, use the class variable tblmarker. This
  50.       // will be set to 1 if EOT/BOT has been reached, 0 otherwise.
  51.  
  52.       err = cust.recnext();     // get next record
  53.       if ( err )
  54.           pdxerr(err, "cust.recnext");
  55.  
  56.       if ( cust.tblmarker )     // if EOT was reached
  57.           break;                // break out of for loop
  58.   }
  59.  
  60.   // Next we will use the search function. Currently search is only
  61.   // provided for Alphanumeric fields but you have the source and
  62.   // can add the appropriate functions. I just haven't ever had to
  63.   // search by date or number yet!
  64.  
  65.   while (1) {       // put in a while loop so you can break out on bad search
  66.       cout << "\nEnter an ID to search for from above list: ";
  67.       cin >> enterid;
  68.  
  69.       // Due to the nature of C++, I have defaulted the last parameter
  70.       // of the search() function to use SEARCHFIRST. For this reason,
  71.       // all you need to do is the following call. If you wanted CLOSESTRECORD
  72.       // cust.custid.search(enterid, CLOSESTRECORD) would be the syntax.
  73.  
  74.       err = cust.custid.search(enterid);
  75.       if ( err ) {
  76.           pdxerr(err, "cust.custid.search");
  77.           break;
  78.       }
  79.  
  80.       // Previously you saw how to use read_rec(). That function will
  81.       // do a PXRecGet() and then do the appropriate PXGet..()'s.
  82.       // Sometimes, you only want to read a field or 2. If speed is a
  83.       // concern then use the method shown below because you don't
  84.       // have to read uneccessary fields.
  85.  
  86.       cust.recget();      // just read record, not any fields
  87.  
  88.       cust.custid.get();  // read custid field
  89.       cust.name1.get();   // read name field
  90.  
  91.       cout << cust.custid << " " << cust.name1 << "\n";
  92.       break;
  93.   };
  94.  
  95.   cust.close();   // close the table and free memory allocated
  96.   PXExit();
  97. }
  98.  
  99. /*    ┌────────────────────────────────────────────────────────────────┐
  100.  *    │                         Error Handler                          │
  101.  *    └────────────────────────────────────────────────────────────────┘  */
  102.  
  103. // This is the standard error handler for PX_Buddy++. You can make it 
  104. // as involved as you like. Since I usually program with Vermont Views
  105. // I have a pdxerr() that has windows that flash errors and have sounds
  106. // etc. Since I don't know what you have, I just made a real generic
  107. // model. pdxerr() does use the C++ property of default parameters.
  108. // What this means is that you can ignore the 3rd (last) parameter
  109. // if you use the prototype:
  110. // void pdxerr(int, char *, int = 0);
  111. // By using this, you never have to specify the 3rd parameter unless
  112. // the error is fatal, which means either the problem must be corrected
  113. // or the program will stop. You can see how I use this in the 
  114. // if (fatal) line.
  115.  
  116. void pdxerr(int err, char *action, int fatal)
  117. {
  118.   char  temp [50];
  119.  
  120.   if (err) {
  121.     gotoxy(0, 23);
  122.     cout << "FATAL ERROR\n";
  123.     cout << err << ": " << action;
  124.  
  125.     cin >> temp;
  126.     if (fatal)
  127.         exit_program();
  128.     return;
  129.   }
  130. }
  131.  
  132.  
  133. // ╓─────────────────────────────────────────────────────────────────────────╖
  134. // ║                      If error occurs, we are here                       ║
  135. // ╚═════════════════════════════════════════════════════════════════════════╝
  136.  
  137. void exit_program()
  138. {
  139.   PXExit();
  140.   exit(1);
  141. }
  142.  
  143.